package com.borderbears.backend.actions;

import java.awt.event.ActionEvent;
import java.util.concurrent.ExecutionException;
import java.util.logging.Logger;

import javax.swing.AbstractAction;
import javax.swing.SwingWorker;

import nanoxml.XMLElement;

import com.borderbears.backend.media.MediaManager;

public class LoadMediaAction extends AbstractAction {

	private final Logger log = Logger.getLogger(getClass().getName());

	public LoadMediaAction() {
		super("Reload Media");
	}

	public void actionPerformed(final ActionEvent e) {
		log.info("Loading Media");
		final SwingWorker<XMLElement, Void> worker = new SwingWorker<XMLElement, Void>() {
			@Override
			protected XMLElement doInBackground() throws Exception {
				return MediaManager.getInstance().loadXMLData();
			}

			@Override
			protected void done() {
				try {
					final XMLElement xml = get();
					if (null != xml) {
						MediaManager.getInstance().setData(xml);
					} else {
						log.warning("The loaded XML was NULL");
					}
				} catch (final InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (final ExecutionException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		};
		worker.execute();
	}

}

